import React from 'react';
import {
    View,
    Text,
    ScrollView,
    TouchableOpacity, Image,
} from 'react-native';
import {
    InputItem,
    List,
} from '@ant-design/react-native';
import Dict from '../../../common/Dict';
import FormItem from "../../../common/components/FormItem";
import FormRadioItem from "../../../common/components/FormRadioItem";
import GlobalStyles from "../../../common/styles/GlobalStyles";
import PickerViewModalCas from '../../../common/components/PickerViewModalCas';
import FontAwesome from "react-native-vector-icons/FontAwesome";
import ToastUtil from "../../../utils/ToastUtil";


export default class ProviderInfoForm extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            sexDict: [],
            idTypeDict: [],
            providerObj: {
                provider:'',
                providerTel:'',
                providerAddress : '',
                providerEmail: '',
                providerGender: '',
                providerGenderLabel: '',
                providerUnit: '',
                unit:{},
                providerUnitName:'',
            },
        };
    }

        //
    async componentDidMount(){
        const gender = await Dict.getDict('dict_gender')

        if(this.state.providerObj.providerGender === '' && gender.length > 0){
            this.state.providerObj.providerGender = gender[0].value
            this.state.providerObj.providerGenderLabel = gender[0].label
        }

        this.setState({
            sexDict: gender
        })
    }
    setData(data){
        this.setState({
            providerObj: data
        })
    }
    _changeFormData(key,value,type){
        let update = {};
        if(type === 'radio'){
            console.log(value)
            update = {
                [key+'Label']: value.label,
                [key]: value.value
            }
        }else if(type === 'select'){
            update = {
                [key]: value.value[0],
                [key+'Label']: value.label[0],
            };
        }else{
            update = { [key]: value};
        }
        const data = Object.assign({}, this.state.providerObj, update);
        this.setState({
            providerObj: data,
        });
        this.props.onChange && this.props.onChange(data)
    }
    render() {
        const providerObj = this.state.providerObj
        return (
            <View style={[GlobalStyles.innerTabPanel,{flexDirection: 'column',flex: 1}]}>
                <ScrollView
                    style={{flex: 1}}
                    automaticallyAdjustContentInsets={false}
                    showsHorizontalScrollIndicator={false}
                    showsVerticalScrollIndicator={false}>
                    <List>
                        <InputItem
                            value={providerObj.provider}
                            maxLength={50}
                            onChange={(value) => this._changeFormData('provider',value)}
                            placeholder="请输入提供人名称">
                            提供人名称
                        </InputItem>
                        <FormRadioItem
                            label={'性别'}
                            checked={[providerObj.providerGender]}
                            data={this.state.sexDict}
                            onChange={(value) => this._changeFormData('providerGender',value, 'radio')}
                        />
                        <InputItem
                            value={providerObj.providerTel}
                            type={'phone'}
                            maxLength={50}
                            onChange={(value) => this._changeFormData('providerTel',value)}
                            placeholder="请输入联系电话">
                            联系电话
                        </InputItem>
                        <InputItem
                            value={providerObj.providerEmail}
                            onChange={(value) => this._changeFormData('providerEmail',value)}
                            placeholder="请输入电子邮箱">
                            电子邮箱
                        </InputItem>
                        <InputItem
                            value={providerObj.providerAddress}
                            maxLength={100}
                            onChange={(value) => this._changeFormData('providerAddress',value)}
                            placeholder="请输入提供人地址">
                            提供人地址
                        </InputItem>
                        <InputItem
                            value={providerObj.providerUnitName}
                            maxLength={50}
                            onChange={(value) => this._changeFormData('providerUnitName',value)}
                            placeholder="请输入单位名称">
                            单位名称
                        </InputItem>
                    </List>
                </ScrollView>
            </View>
        );
    }
}
